from django.db import models
from django.conf import settings
from django.db.models.signals import post_save
from django.dispatch import receiver
from mdeditor.fields import MDTextField


# 论坛数据库表
class Category(models.Model):
    # 自动生成类别ID列
    categoryName = models.CharField(max_length=50, verbose_name="类别名")
    tab = models.CharField(verbose_name="类别标签", max_length=50)
    clickNumber = models.IntegerField(verbose_name="点击量", default=0)
    containNumber = models.IntegerField(default=0, verbose_name="包含贴子数")
    publishTime = models.DateTimeField(auto_now_add=True, verbose_name="类别公布时间")
    updateTime = models.DateTimeField(auto_now_add=True, verbose_name="类别更新时间")

    # UserId = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name="发起者", related_name='category')

    def __str__(self):
        return self.categoryName


# 标签表
class Tag(models.Model):
    name = models.CharField(max_length=50, unique=True, verbose_name="标签名称")

    def __str__(self):
        return self.name


# 帖子表
class Posts(models.Model):
    # 自动生成帖子ID列
    title = models.CharField(max_length=100, verbose_name="帖子标题")
    content = MDTextField(verbose_name="帖子内容")
    publishTime = models.DateTimeField(auto_now_add=True, verbose_name="发布时间")
    stowNumber = models.IntegerField(default=0, verbose_name="收藏数")
    browseNumber = models.IntegerField(default=0, verbose_name="浏览量")
    replyNumber = models.IntegerField(default=0, verbose_name="回复量")
    likeNumber = models.IntegerField(default=0, verbose_name="点赞量")
    UserId = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name="作者",
                               related_name='posts')
    category = models.ForeignKey(Category, on_delete=models.CASCADE, verbose_name="帖子类别",
                                 related_name='posts_type')  # 记得填类别表
    tags = models.ManyToManyField(Tag, verbose_name="帖子标签")
    # resource = models.OneToOneField(WebsiteModels.Resource, on_delete=models.CASCADE, verbose_name="资源编号", related_name='resource_post')


# 评论表
class Remark(models.Model):
    # 自动生成评论ID列
    content = models.TextField(help_text="评论内容")
    publishTime = models.DateTimeField(auto_now_add=True, verbose_name="发布时间")
    likesNumber = models.IntegerField(default=0, verbose_name="点赞数")
    dislikesNumber = models.IntegerField(default=0, verbose_name="被踩次数")
    postId = models.ForeignKey(Posts, on_delete=models.CASCADE, verbose_name="所属帖子", related_name='remark')
    UserId = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name="发起者",
                               related_name='remark_user')


# 回复表
class Reply(models.Model):
    content = models.TextField(verbose_name="回复内容")
    author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name="回复作者")
    UserId = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name="回复对象",
                               related_name='reply_to')
    # reply_to_user = models.ForeignKey('self', null=True, blank=True, on_delete=models.SET_NULL, verbose_name="回复对象",
    #                                   related_name='reply_to')
    remarkId = models.ForeignKey(Remark, on_delete=models.CASCADE, related_name="replies", verbose_name="所属评论")
    postId = models.ForeignKey(Posts, on_delete=models.CASCADE, verbose_name="所属帖子", related_name='reply_to_post')
    publishTime = models.DateTimeField(auto_now_add=True, verbose_name="回复时间")
    likes_number = models.IntegerField(default=0, verbose_name="点赞数")
    dislikes_number = models.IntegerField(default=0, verbose_name="踩数")



